<?php
	require_once APPPATH.'third_party/admin_controller'.EXT;
	class Photo extends Admin_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function list_cats()
        {
            $this->check_login_admin();
            $data = array();
            $data['cats'] = $this->photo_model->list_cats();
            $this->load_render($this->load->view('gallery/list_cats',$data,TRUE));        
        }
        public function list_photo()
        {
            $this->check_login_admin();
            $data = array();
            $data['photo'] = $this->photo_model->list_photo();
            $data['cats'] = $this->photo_model->list_cats();
            $this->load_render($this->load->view('gallery/list_photo',$data,TRUE));
        }
        public function add_update_cats()
        {
            $this->check_login_admin();
            $data = array();
            $id = $this->uri->segment('4');          
            $valid = $this->form_validation;
            $valid->set_rules('txt_title','Tiêu đề','trim|required|xss_clean');
            if($valid->run() == TRUE)
            {
                $title = $this->input->post('txt_title');               
                $url_page = $this->util->alias($title);
                $meta_keyword = $this->input->post('txt_meta_keyword');
                $meta_description = $this->input->post('txt_meta_description');               
                $order = $this->input->post('txt_order');
                $active = $this->input->post('txt_active');                
                $data = array(
                'title_cats' => $title,
                'url_title_cats'   => $url_page,
                'meta_keyword'   => $meta_keyword,
                'meta_description'   => $meta_description,
                'active'   => $active,
                'order_cats'   => $order,
                    );
                if($id != '')
                 {
        
                        if($this->photo_model->update_cats($id,$data))
                        {
                            $data['error'] = "Cập nhật thành công";
                        }
                        else
                        {
                            $data['error'] = "Cập nhật không thành công";
                        }
                        redirect('admin/photo/list_cats');
                   
                 }
                 else
                 {
                   
                        if($this->photo_model->add_cats($data))
                        {
                            $data['error'] = "Thêm thành công";
                        }
                        else
                        {
                            $data['error'] = "Thêm không thành công";
                        }
                        redirect('admin/photo/list_cats');
                 }                           
            }
            $data['cats'] = $this->photo_model->get_by_cats($id);
            $data['uid'] = $id;
            $this->load_render($this->load->view('gallery/add_cats',$data,TRUE));
        }
        public function add_update_photo()
        {
            $this->check_login_admin();
            $data = array();           
            if(@$_FILES['uploaded_files']['name']!='')
     		{
     		 foreach($_FILES['uploaded_files']['name'] as $key => $value)
	           {
	               if(is_uploaded_file($_FILES['uploaded_files']['tmp_name'][$key]) &&	$_FILES['uploaded_files']['error'][$key] == 0)
                     {
	                $filename = $_FILES['uploaded_files']['name'][$key];
                    $filename = time().rand(0,999).$filename;
				    $id_cats = htmlspecialchars(trim($_POST['txt_select'][$key]));
                    $title = htmlspecialchars(trim($_POST['name_files'][$key]));
                    $url_title = $this->util->alias($title);
                    $order = (htmlspecialchars(trim($_POST['txt_order'][$key]))) ? htmlspecialchars(trim($_POST['txt_order'][$key])): 0;
                    $active = (htmlspecialchars(trim($_POST['txt_active'][$key]))) ? htmlspecialchars(trim($_POST['txt_active'][$key])) : 0;
                    
					if(move_uploaded_file($_FILES['uploaded_files']['tmp_name'][$key], 'upload/photo/'. $filename))
                        {   
                           $data = array(
                                'name_photo' => $title,
                                'name_url'   => $url_title,
                                'image'   => 'upload/photo/'.$filename,
                                'active'   => $active,
                                'order_photo'   => $order,
                                'id_cats'   => $id_cats,
                            );
                            if($this->photo_model->add_photo($data))
                            {
                               $data['error'] = 'Upload thành công<br/>'; 
                            }
                            
                        }
                        else
            			{
            		
            				$data['error'] =  'Upload khong thanh cong';
                           
                        }
				}
             }
		  }		
            $data['cats'] = $this->photo_model->list_cats();
            $this->load_render($this->load->view('gallery/add_photos',$data,TRUE));
        }
        function edit_photo()
        {
            $this->check_login_admin();
            $data = array();    
            $id = $this->uri->segment('4');
            
            $image = $this->input->post('image');
             $valid = $this->form_validation;
             $valid->set_rules('txt_title','Tiêu đề','trim|required|xss_clean'); 
             if($valid->run() == TRUE)
             {
                $data = array(
                        'name_photo' => $this->input->post('txt_title'),
                        'name_url'   => $this->util->alias($this->input->post('txt_title')),
                        
                        'active'   => $this->input->post('txt_active'),
                        'order_photo'   => $this->input->post('txt_order'),
                        'id_cats'   => $this->input->post('txt_id_cats'),
                    );
                if($_FILES['p_image']['name']!='')
				{
					$config['upload_path'] = './upload/photo/';
					$config['allowed_types'] = 'gif|jpg|png|jpeg';
					$config['remove_spaces'] = TRUE;
					$config['max_size'] = 1024;
					$config['max_width'] = '1024';
					$config['max_height'] = '1024';
					$config['file_name'] = $_FILES['p_image']['name'];
					$this->load->library('upload', $config);
					if(!$this->upload->do_upload('p_image')) 
					{
					$data['error'] = $this->upload->display_error();
					} else {
					$upload = $this->upload->data();
					$data['image'] = 'upload/photo/'.$upload['file_name'];					
						if($image !='')
						{
						@unlink($image);					
						}
					
					}	
				}
				else
				{
					if($image !='') {
					$data['image'] = $image;				
					} else {
						$data['image'] = '';					
					}
				}
                if($id!='')
                {
                    
                    if($this->photo_model->update_photo($id,$data))
                    {
                        $data['error'] = "Cập nhật thành công";
                        redirect('admin/photo/list_photo');
                    }
                }
             }
            $data['cats'] = $this->photo_model->list_cats();     
            $data['photo'] = $this->photo_model->get_by_photo($id);
            $data['uid'] = $id;          
            $this->load_render($this->load->view('gallery/edit_photo',$data,TRUE));
           
        }
        public function delete_cats()
        {
             $this->check_login_admin();
             $id = $this->uri->segment('4');
             if($this->photo_model->delete_cats($id))
             {
                $this->photo_model->delete_photo($id);
                redirect('admin/photo/list_cats');
             }
        }
        public function delete_photo()
        {
             $this->check_login_admin();
             
            
             
                $id = $this->uri->segment('4');
             
             if($this->photo_model->delete_photo($id))
             {
                redirect('admin/photo/list_photo');
             }
             
        }
        function ajax_delete_all_photo()
        {
            $this->check_login_admin();
            $data = array();
                $id = explode(",",$this->input->get('id'));     
                foreach($id as $key)
                {
                   if($this->photo_model->delete_all_photo_cats($key))
                     {
                        $data['error'] =  "Xóa thành công";                   
                     } 
                }                
                echo json_encode($data);
        }
        public function change_active_cats()
        {
             $this->check_login_admin();
            $data = array();            
            $id = $this->input->get('id');
            $data['active'] = $this->input->get('active');
            if($this->photo_model->change_update_active_cats($id,$data))
            {
                $data['error'] = "Cập nhật thành công";
            }
            else
            {
                $data['error'] = "Cập nhật không thành công";
            }
            
            echo json_encode($data);
        }
        public function change_active_photo()
        {
             $this->check_login_admin();
            $data = array();            
            $id = $this->input->get('id');
            $data['active'] = $this->input->get('active');
            if($this->photo_model->change_update_active_photo($id,$data))
            {
                $data['error'] = "Cập nhật thành công";
            }
            else
            {
                $data['error'] = "Cập nhật không thành công";
            }
            
            echo json_encode($data);
        }
        function ajax_photo()
        {
            $this->check_login_admin();
            $data = array();
            $id = $this->input->get('id');
            $uid = $id;
            $data['photo'] = $this->photo_model->get_by_id_photo($id);
            $this->load->view('gallery/ajax_photo',$data);
        }
   }
?>   